function getXmlHttpRequestObject() {
        if (window.XMLHttpRequest) {
                return new XMLHttpRequest();
        } else if (window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP");
        } else {
                alert("IE5/6 not supported");
        }
}

var searchReq = getXmlHttpRequestObject();

function searchSuggest() {
        if (searchReq.readyState == 4 || searchReq.readyState == 0) {
                var str = escape(document.getElementById('tag').value);
                searchReq.open("GET", '/suggest?sandbox=true&method=gettypeahead&text='+ str+'&lang=en&field=WHAT&UID=bobsyouruncle', true);
                searchReq.onreadystatechange = handleSearchSuggest; 
                searchReq.send(null);
        }               
}

function handleSearchSuggest() {
        if (searchReq.readyState == 4) {
                var ss = document.getElementById('tag_suggest')
                ss.innerHTML = '';
                var str = searchReq.responseText.split("\n");
                for(i=0; i < str.length - 1; i++) {
                        var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
                        suggest += 'onmouseout="javascript:suggestOut(this);" ';
                        suggest += 'onclick="javascript:append(this.innerHTML);" ';
                              
                        suggest += 'class="suggest_link">' + str[i] + '</div>';
                        ss.innerHTML += suggest;
                }
        }
}

function suggestOver(div_value) {
        div_value.className = 'suggest_link_over';
}

function suggestOut(div_value) {
        div_value.className = 'suggest_link';
}

function append(str) {
	document.getElementById('tag_div').innerHTML += "<div class='tag'>"+str+"</div>";
	document.getElementById('tagVals').value += str+", ";
}
